define(['layui','message'], function () {
    var laydate = layui.laydate
        , $body = $('body');
    /*! 全局文件上传入口 */
    $.fn.uploadFile = function (callback) {
        if (this.attr('data-inited')) return false;
        var that = this, mode = $(this).attr('data-file') || 'one';
        this.attr('data-inited', true);
        this.attr('data-multiple', (mode !== 'btn' && mode !== 'one') ? 1 : 0);
        require(['upload'], function (apply) {
            apply(that, null, callback);
        });
    };

    /*! 上传单个图片 */
    $.fn.uploadOneImage = function () {
        var name = $(this).attr('name') || 'image', type = $(this).data('type') || 'png,jpg,gif';
        // var $tpl = $('<a data-file="btn" class="uploadimage"></a>').attr('data-field', name).attr('data-type', type);
        var after = $('<a class="uploadimage"></a>').attr('data-file', 'btn').attr('data-field', name).attr('data-type', type);
        var bofore = $('<div class="uploadimage uploadimagemtl"></div>');
        $(this).attr('name', name).before(bofore).after(after).on('change', function () {
            bofore.attr('data-tips-image', this.value).css('backgroundImage', 'url(' + this.value + ')');
        }).trigger('change');
    };

    /*! 以button形式上传单个图片 */
    $.fn.uploadButtonOneImage = function () {
        var name = $(this).attr('name') || 'image', type = $(this).data('type') || 'png,jpg,gif';
        var after = $('<a class="layui-btn layui-btn-primary"><i class="layui-icon font-s14 layui-icon-upload-drag"></i> 上传图片</a>').attr('data-file', 'btn').attr('data-field', name).attr('data-type', type);
        var bofore = $('<div class="uploadimage uploadimagemtl"></div>');
        $(this).attr('name', name).before(bofore).after(after).on('change', function () {
            if (!this.value) {
                bofore.hide();
            }else{
                bofore.attr('data-tips-image', this.value).css('backgroundImage', 'url(' + this.value + ')').show();
            }

        }).trigger('change');
    };
    /*! 上传多个图片 */
    $.fn.uploadButtonMultipleImage = function () {
        var type = $(this).data('type') || 'png,jpg,gif', name = $(this).attr('name') || 'umt-image';
        var $tpl = $('<a class="layui-btn layui-btn-primary"><i class="layui-icon font-s14 layui-icon-upload-drag"></i> 上传图片</a>').attr('data-file', 'mul').attr('data-field', name).attr('data-type', type);
        $(this).attr('name', name).after($tpl.data('input', this)).on('change', function () {
            var input = this;
            this.setImageData = function () {
                input.value = input.getImageData().join(',');
            };
            this.getImageData = function () {
                var values = [];
                $(input).prevAll('.uploadimage').map(function () {
                    values.push($(this).attr('data-tips-image'));
                });
                return values.reverse(), values;
            };
            var urls = this.getImageData(), srcs = this.value ? this.value.split(',') : '';
            for (var i in srcs) if (srcs[i]) urls.push(srcs[i]);
            $(this).prevAll('.uploadimage').remove();
            this.value = urls.join(',');
            for (var i in urls) {
                var tpl = '<div class="uploadimage uploadimagemtl"><a class="layui-icon margin-right-5">&#xe602;</a><a class="layui-icon margin-right-5">&#x1006;</a><a class="layui-icon margin-right-5">&#xe603;</a></div>';
                var $tpl = $(tpl).attr('data-tips-image', urls[i]).css('backgroundImage', 'url(' + urls[i] + ')').on('click', 'a', function (e) {
                    e.stopPropagation();
                    var $cur = $(this).parent();
                    switch ($(this).index()) {
                        case 1:// remove
                            return $.msg.confirm('确定要移除这张图片吗？', function (index) {
                                $cur.remove(), input.setImageData(), $.msg.close(index);
                            });
                        case 0: // right
                            var lenght = $cur.siblings('div.uploadimagemtl').length;
                            if ($cur.index() !== lenght) $cur.next().after($cur);
                            return input.setImageData();
                        case 2: // left
                            if ($cur.index() !== 0) $cur.prev().before($cur);
                            return input.setImageData();
                    }
                });
                $(this).before($tpl);
            }
        }).trigger('change');
    };
    /*! 上传单个图片 */
    $.fn.uploadOneImage = function () {
        var name = $(this).attr('name') || 'image', type = $(this).data('type') || 'png,jpg,gif';
        var $tpl = $('<a data-file="btn" class="uploadimage"></a>').attr('data-field', name).attr('data-type', type);
        $(this).attr('name', name).after($tpl.data('input', this)).on('change', function () {
            if (this.value) $tpl.css('backgroundImage', 'url(' + this.value + ')');
        }).trigger('change');
    };
    /*! 上传多个图片 */
    $.fn.uploadMultipleImage = function () {
        var type = $(this).data('type') || 'png,jpg,gif', name = $(this).attr('name') || 'umt-image',limit = $(this).data('limit') || 10;
        var $tpl = $('<a class="uploadimage"></a>').attr('data-file', 'mul').attr('data-field', name).attr('data-type', type);
        $(this).attr('name', name).after($tpl.data('input', this)).on('change', function () {
            var input = this;
            this.setImageData = function () {
                input.value = input.getImageData().join('|');
            };
            this.getImageData = function () {
                var values = [];
                $(input).prevAll('.uploadimage').map(function () {
                    values.push($(this).attr('data-tips-image'));
                });
                return values.reverse(), values;
            };
            if(this.getImageData().length >= limit){
                return $.msg.error('图片上传已达上限！');
            }
            var urls = this.getImageData();
            var srcs = this.value ? this.value.split('|') : '';
            if(this.value == this.value.split('|')){
                srcs = this.value ? this.value.split(',') : '';
            }
            for (var i in srcs) if (srcs[i]) urls.push(srcs[i]);
            $(this).prevAll('.uploadimage').remove();
            this.value = urls.join('|');
            for (var i in urls) {
                var tpl = '<div class="uploadimage uploadimagemtl"><a class="layui-icon margin-right-5">&#xe602;</a><a class="layui-icon margin-right-5">&#x1006;</a><a class="layui-icon margin-right-5">&#xe603;</a></div>';
                var $tpl = $(tpl).attr('data-tips-image', urls[i]).css('backgroundImage', 'url(' + urls[i] + ')').on('click', 'a', function (e) {
                    e.stopPropagation();
                    var $cur = $(this).parent();
                    switch ($(this).index()) {
                        case 1:// remove
                            return $.msg.confirm('确定要移除这张图片吗？', function (index) {
                                $cur.remove(), input.setImageData(), $.msg.close(index);
                            });
                        case 0: // right
                            var lenght = $cur.siblings('div.uploadimagemtl').length;
                            if ($cur.index() !== lenght) $cur.next().after($cur);
                            return input.setImageData();
                        case 2: // left
                            if ($cur.index() !== 0) $cur.prev().before($cur);
                            return input.setImageData();
                    }
                });
                $(this).before($tpl);
            }
        }).trigger('change');
    };

    $.fn.uploadOneVideo = function () {
        var name = $(this).attr('name') || 'video', type = $(this).data('type') || 'mp4,flv,wmv';
        var $tpl = $('<a data-file="btn" style="width:320px;height: 240px; " class="uploadimage"><video width="320" height="240" controls></video></a>').attr('data-field', name).attr('data-type', type);
        $(this).attr('name', name).after($tpl.data('input', this)).on('change', function () {
            if (this.value) $tpl.append('<source src="" type="video/mp4">',).attr('src', this.value);
        }).trigger('change');
    };
    $.fn.uploadMultipleVideo= function () {
        var type = $(this).data('type') || 'mp4,flv,wmv', name = $(this).attr('name') || 'umt-image',limit = $(this).data('limit') || 10;
        var $tpl = $('<a class="uploadimage"></a>').attr('data-file', 'mul').attr('data-field', name).attr('data-type', type);
        $(this).attr('name', name).after($tpl.data('input', this)).on('change', function () {
            var input = this;
            this.setImageData = function () {
                input.value = input.getImageData().join('|');
            };
            this.getImageData = function () {
                var values = [];
                $(input).prevAll('.uploadimage').map(function () {
                    values.push($(this).attr('data-tips-image'));
                });
                return values.reverse(), values;
            };
            if(this.getImageData().length >= limit){
                return $.msg.error('视频上传已达上限！');
            }
            var urls = this.getImageData();
            var srcs = this.value ? this.value.split('|') : '';
            if(this.value == this.value.split('|')){
                srcs = this.value ? this.value.split(',') : '';
            }
            for (var i in srcs) if (srcs[i]) urls.push(srcs[i]);
            $(this).prevAll('.uploadimage').remove();
            this.value = urls.join('|');
            for (var i in urls) {
                var tpl = '<div class="uploadimage uploadimagemtl" style="width:320px;height: 240px; ">' +
                    '<a class="layui-icon margin-right-5">&#xe602;</a>' +
                    '<a class="layui-icon margin-right-5">&#x1006;</a>' +
                    '<a class="layui-icon margin-right-5">&#xe603;</a>' +
                    '<video width="320" height="240" controls></video>'+
                    '</div>';
                var $tpl = $(tpl).attr('data-tips-image', urls[i]).css('backgroundImage', 'url(' + urls[i] + ')')
                    .children('video')
                    .append('<source src="" type="video/mp4">',).attr('src', urls[i]).parent()
                    .on('click', 'a', function (e) {
                    e.stopPropagation();
                    var $cur = $(this).parent();
                    switch ($(this).index()) {
                        case 1:// remove
                            return $.msg.confirm('确定要移除视频吗？', function (index) {
                                $cur.remove(), input.setImageData(), $.msg.close(index);
                            });
                        case 0: // right
                            var lenght = $cur.siblings('div.uploadimagemtl').length;
                            if ($cur.index() !== lenght) $cur.next().after($cur);
                            return input.setImageData();
                        case 2: // left
                            if ($cur.index() !== 0) $cur.prev().before($cur);
                            return input.setImageData();
                    }
                });
                $(this).before($tpl);
            }
        }).trigger('change');
    };
});